<?php
function ListAllNews($idP=0)
{
    global $dbsql,$language,$show_page;
    
    $sql_where  = " from tblnews_content where iStatus=1 and cLanguage='$language' ";
    if($idP>0)
    {
        $sql_where .= " and iParentId=$idP ";
    }
    
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    list($lf,$la)= Split_Page($result['count']);
    
    $sql_select = "select iId,cUrlString,cImage,cName,cDescription,dPostDate,iParentId ".$sql_where." order by iOrder limit $lf,$la";
    $rs         = $dbsql->query($sql_select);
    $rows       = $dbsql->num_rows($rs);
    if($rows==0)
    {
        return '<div class="notfound">'.getLang("Not found result").'</div>';
    }
    $html       = '<div id="ajaxPage">';
    while($result=$dbsql->fetch_array($rs))
    {
        $link   = getLinkNews($result['iParentId'],$result['cUrlString']);
        $imglink= GetIntroImg($result['cImage'],true,false);
        
        $html  .= '<div class="list">';
        if($imglink)
        {
            $html  .= '<a class="img" href="'.echoLink($link,false).'"><img src="'.($imglink).'" width=100% border=0/></a>';
        }
        $html  .= '<a class="ttn" href="'.echoLink($link,false).'">'.$result['cName'].'</a>';
        $html  .= '<span class="msg">'.$result['cDescription'].'</span>';
        $html  .= '<div class="clear" align=right>&raquo; <a class="more" href="'.echoLink($link,false).'">'.getLang('read more').'</a></div>';
        $html  .= '</div>';
    }
    $html   .= $show_page;
    $html   .= '</div>';
    return $html;
}

function getLinkNews($idP,$url='')
{
    global $dbsql,$language,$urlsParentNews;
    
    if(!isset($urlsParentNews[$idP]))
    {
        $sql_select = "select cUrlString,iParentId from tblcategory where iId='$idP' and cLanguage='$language'";
        $result     = $dbsql->query_fetch($sql_select);
        if($result['iParentId']>0)
        {
            $urlP   = getLinkNews($result['iParentId']).'/';
        }else
        {
            $urlP   = '';
        }    
        $urlsParentNews[$idP]   = $urlP.$result['cUrlString'];
        
    }
    $link = (empty($url))? $urlsParentNews[$idP]: $urlsParentNews[$idP].'/'.$url.'.htm';
    return $link;
}

function ListLatestNews($limit=5)
{
    global $dbsql,$language;
    
    $sql_select = "select cName,cUrlString,iParentId from tblnews_content where iStatus=1 and cLanguage='$language'
                        order by iId desc limit $limit";
    return $dbsql->load($sql_select);
}

function OtherNews($idP,$id,$limit=10)
{
    global $dbsql,$language;
    
    $sql_select = "select cName,cUrlString,iParentId,dPostDate from tblnews_content where iParentId='$idP' and iId!='$id' 
                    and cLanguage='$language' and iStatus=1 order by iId desc limit $limit";
    $rs         = $dbsql->query($sql_select);
    if($dbsql->num_rows($rs)==0)
    {
        return;
    }
    $html       = '<div class="othernews">'.getLang('Other News').'</div>';
    while($result= $dbsql->fetch_array($rs))
    {
        $link    = getLinkNews($result['iParentId'],$result['cUrlString']);
        
        $html   .= '<li class="othernews"><a href="'.echoLink($link,false).'">'.$result['cName'].'</a> ';
        $html   .= ' <span> ('.gpc_DisplayDate2($result['dPostDate'],"d/m/Y").')</span></li>';
    }
    return $html;
}
?>